package com.mobisys.live.controller;

import com.mobisys.live.bean.channel.GetAllLiveList;
import com.mobisys.live.bean.channel.GetChannelVideoList;
import com.mobisys.live.bean.video.DeleteVideo;
import com.mobisys.live.bean.video.GetAllVideoList;
import com.mobisys.live.dao.mapper.VideoMapper;
import com.mobisys.live.dao.model.Video;
import com.mobisys.live.response.Response;
import com.mobisys.live.response.channel.VideoResponseBody;
import com.mobisys.live.util.TagUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import static com.mobisys.live.config.ResponseCode.*;

/**
 * Created by zhuo on 2018-01-16.
 * 有关点播相关的接口
 */
@RestController
public class VodController {

    @Autowired
    private VideoMapper videoMapper;

    /**
     * 分页获取媒体库里的所有的视频
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/0.1/video/list",method = RequestMethod.GET)
    public Response getVideoList(@RequestParam String userId,@RequestParam int page, @RequestParam int size,
                                 @RequestParam String type, @RequestParam String sort,HttpServletRequest httpServletRequest){
        Response response=new Response<List<VideoResponseBody>>();

        response.setRetureValue(INPUT_IS_INVALID);
        response.setRetureMsg(MSG_INPUT_IS_INVALID);
        if(userId==null||page<=0){
            return response;
        }

        if(size<=0||size>10){
            size=10;
        }
        page=(page-1)*10;

        type=null;
        sort=null;

        //获取点播视频
        List<Video> videoList=videoMapper.selectByUserIdByPage(userId,page,size,type,sort);
        List<VideoResponseBody> list=new ArrayList<>();
        for (int i = 0; i < videoList.size(); i++) {
            Video video=videoList.get(i);
            VideoResponseBody videoResponseBody=new VideoResponseBody();
            videoResponseBody.setType(video.getType());
            videoResponseBody.setIntroduction(video.getIntroduction());
            videoResponseBody.setVideoId(video.getId());
            videoResponseBody.setTitle(video.getTitle());
            videoResponseBody.setUserId(video.getUserId());

            videoResponseBody.setViewCount(video.getViewCount());
            videoResponseBody.setLikeCount(video.getLikeCount());
            videoResponseBody.setPlayUrl(video.getPlayUrl());

            List<String> taglist=TagUtil.convertIdStrToTags(video.getTag());
            videoResponseBody.setTag(taglist);

            list.add(videoResponseBody);

        }

        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(list);
        return response;

    }


    /**
     * 删除视频
     * @param videoId
     * @param userId
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/video/{videoId}",method = RequestMethod.DELETE)
    public Response deleteVideo(@PathVariable String videoId,@RequestParam String userId, HttpServletRequest httpServletRequest){
        Response response=new Response();
        /*//检验输入
        if(bindingResult.hasErrors()){

        }*/

        if(videoId==null||videoId.isEmpty()){
            response.setRetureValue(INPUT_IS_INVALID);
            response.setRetureMsg(MSG_INPUT_IS_INVALID);
            return response;
        }

        int result=videoMapper.deleteVideo(videoId,(byte) 1);
        if(result>0){
            response.setRetureValue(SUCCESS);
            response.setRetureMsg(MSG_SUCCESS);
        }else {
            response.setRetureValue(DELETE_VIDEO_ERROR);
            response.setRetureMsg(MSG_DELETE_VIDEO_ERROR);
        }


        return response;

    }

    /**
     * 获取某个频道里面的视频列表
     * @param getChannelVideoList
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/video/getVideoInChannel",method = RequestMethod.POST)
    public Response getVideoList(@RequestBody GetChannelVideoList getChannelVideoList, HttpServletRequest httpServletRequest){
        Response response=new Response<List<VideoResponseBody>>();
        Timer timer=new Timer();
        timer.cancel();
        //校验输入

        //
        List<Video> list=videoMapper.selectByChannelId(getChannelVideoList.getChannelId());
        List<VideoResponseBody> videoResponseBodyList=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Video video=list.get(i);
            VideoResponseBody videoResponseBody=new VideoResponseBody();

            videoResponseBody.setUserId(video.getUserId());
            videoResponseBody.setVideoId(video.getId());
            videoResponseBody.setTitle(video.getTitle());
            videoResponseBody.setIntroduction(video.getIntroduction());
            videoResponseBody.setType(video.getType());
            videoResponseBody.setViewCount(video.getViewCount());
            videoResponseBody.setLikeCount(video.getLikeCount());
            videoResponseBody.setPlayUrl(video.getPlayUrl());

            List<String> tag=new ArrayList<>();

            //查表获取tag

            videoResponseBody.setTag(tag);

            videoResponseBodyList.add(videoResponseBody);
        }

        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(videoResponseBodyList);


        return response;
    }


}
